package servlet;

import com.db.conn;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;



public class placard_List extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public placard_List() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	//根据条件返回通知公告信息
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		PrintWriter out = response.getWriter();
		JSONArray jsonarray = new JSONArray();
		JSONObject jsonobj = new JSONObject();


		conn cn = new conn();
	
	    String f=request.getParameter("f");

		ResultSet rs=null;

		if(f.equals("1"))
		{
			rs=cn.query("select * from  placard order by pid desc limit 0,5");
		}
		else if(f.equals("2"))
		{
			rs=cn.query("select * from  placard order by pid desc ");
		}
		else if(f.equals("3"))
		{
			String id = request.getParameter("id");
			rs=cn.query("select * from  placard  where pid="+id);
		}

		while (true)
		{
			try {
				if (!rs.next()) break;
             jsonobj.put("pid", rs.getString("pid"));
             jsonobj.put("ptitle", rs.getString("ptitle"));
             jsonobj.put("pmemo", rs.getString("pmemo").replaceAll(request.getContextPath(),"http://localhost:8080/"+request.getContextPath()).replaceAll("<img","<img style=='width:100%' "));
             jsonobj.put("ptime", rs.getString("ptime"));
		
				jsonarray.add(jsonobj);

			} catch (SQLException e) {
				e.printStackTrace();
			}

		}

		out.println(jsonarray);	
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

